package com.tuojiahr.tools;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * 分页过滤器
 * 
 * @author wang.haibo
 * @date 2012-11-7 下午10:09:46
 */
public class PagerFilter implements Filter {

	public void destroy() {
		
	}

	private int getOffset(HttpServletRequest request){
		int offset = 0;
		String poffset = request.getParameter("pager.offset");
		if(StringUtil.isNotBlank(poffset)) {
			offset = Integer.parseInt(poffset);
		}
		return offset;
	}
	
	private int getPagesize(){
		return 10;
	}
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest myrequest = (HttpServletRequest)request;
		PageUtil.setOffset(this.getOffset(myrequest));
		PageUtil.setPagesize(this.getPagesize());
		try{
			chain.doFilter(request, response);
		}catch(Exception e){
			
		}finally{
			PageUtil.removeOffset();
			PageUtil.removePagesize();
		}
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		
	}
}
